package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import flex01.SocketServer;

import modle.Player;
/**
 * @描述 在用户首次进入房间时，加载所有的玩家信息。返回JSON格式，Player的集合
 * @author 高通
 * @date 2013/1/5
 */
public class GetRoomPlayersServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/javascript;charset=utf-8");
		 response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		int RoomIndex = Integer.parseInt(request.getParameter("selectedRoom"));
		SocketServer server = StartSocketServlet.socketServerList.get(RoomIndex);
		Player players[] = new Player[server.clientList.size()];
		for(int i=0; i<players.length; i++){
			
			players[i] = new Player();
			players[i].setName(server.clientList.get(i).getName());
			players[i].setTablePosition(server.clientList.get(i).getTablePosition());
		}
		Gson gson = new Gson();
		out.println(gson.toJson(players));
		System.out.println("tables and players:" + gson.toJson(players));
		out.flush();
		out.close();
	}

}
